package com.szqhyxj.youxiangju.biz.merchantHome.shop;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BasePromptFragment;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.PosCommodity;
import com.szqhyxj.youxiangju.biz.merchantHome.my.MyPresenter;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.adapter.CommoditeDeviceAdapter;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.commoditeDetails.CommoditeDeviceDetailsActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class ShopFragment extends BasePromptFragment implements ShopContract.View, CommoditeDeviceAdapter.OnItemClickListener {

    @BindView(R.id.creaditDetails)
    ListView creaditDetails;
    Unbinder unbinder;

    private CommoditeDeviceAdapter adapter;

    public static ShopFragment newInstance() {
        Bundle args = new Bundle();
        ShopFragment fragment = new ShopFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shop;
    }

    @Override
    public void showProductList(List<PosCommodity> data) {
        adapter = new CommoditeDeviceAdapter(getContext(), data);
        adapter.setListener(this);
        creaditDetails.setVisibility(View.VISIBLE);
        creaditDetails.setAdapter(adapter);
    }

    @Override
    public void showToast(String s) {
        Toast.makeText(getActivity(), s, Toast.LENGTH_LONG).show();
    }

    ShopPresenter presenter;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (presenter == null) {
                presenter = new ShopPresenter(this);
                if (UserInfoManger.INSTANCE.getUser().getPartnerActivity() == 0) {
                    presenter.init();
                } else {
                    if (UserInfoManger.INSTANCE.getUser().getUserType() == 4 || UserInfoManger.INSTANCE.getUser().getUserType() == 5) {
                        presenter.init();
                    } else {

                    }
                }


            } else {
                if (UserInfoManger.INSTANCE.getUser().getPartnerActivity() == 0) {
                    presenter.init();
                } else {
                    if (UserInfoManger.INSTANCE.getUser().getUserType() == 4 || UserInfoManger.INSTANCE.getUser().getUserType() == 5) {
                        presenter.init();
                    } else {

                    }
                }
            }
        }
    }

    @Override
    protected void start() {


    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(String packageId) {
        CommoditeDeviceDetailsActivity.start(getActivity(), packageId);
    }

}